﻿using System;
using System.Diagnostics;
using System.IO;
using System.Xml;

namespace PropertyExpression.ReactiveRelation.SynchronizationWithDbSchema
{
    class Program
    {
        static void Main()
        {
            new MyClass().qwe();
        }
    }

    public class MyClass
    {
        public void qwe()
        {
            qwe4();

            var xmlDocument = new XmlDocument();

            var path = string.Format(@"{0}\..\..\..\ReactiveRelation.Test.Demo\Class1.dbml",
                                     Path.GetDirectoryName(GetType().Assembly.Location));
            xmlDocument.Load(path);

            var xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
            xmlNamespaceManager.AddNamespace("a", @"http://schemas.microsoft.com/linqtosql/dbml/2007");

            foreach (XmlNode node in xmlDocument.SelectNodes("/a:Database/a:Function", xmlNamespaceManager))
            {
                string value = node.Attributes["Name"].Value;
                if (value == "dbo.fn_diagramobjects"
                    || value == "dbo.sp_alterdiagram"
                    || value == "dbo.sp_creatediagram"
                    || value == "dbo.sp_dropdiagram"
                    || value == "dbo.sp_helpdiagramdefinition"
                    || value == "dbo.sp_helpdiagrams"
                    || value == "dbo.sp_renamediagram"
                    )
                {
                    node.ParentNode.RemoveChild(node);
                }
            }

            xmlDocument.Save(path);


            qwe5();
        }

        private void qwe4()
        {
            var process =
                new Process
                    {
                        StartInfo =
                            {
                                UseShellExecute = false,
                                RedirectStandardOutput = true,
                                FileName = @"C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\SqlMetal.exe",
                                Arguments =
                                    string.Format(@"/conn:""Data Source=(local);Initial Catalog=ReactiveRelationDemoTest;Integrated Security=True"" /dbml:{0}\..\..\..\ReactiveRelation.Test.Demo\Class1.dbml /language:csharp /namespace:PropertyExpression.ReactiveRelation.Test.Demo /context:DataClassesDataContext /pluralize /views /functions /sprocs",
                                                  Path.GetDirectoryName(GetType().Assembly.Location))
                            }
                    };
            process.Start();
            var readToEnd = process.StandardOutput.ReadToEnd();
            if (!string.IsNullOrEmpty(readToEnd))
            {
                if (!"Microsoft (R) Database Mapping Generator 2008 version 1.00.30729\nfor Microsoft (R) .NET Framework version 3.5\r\nCopyright (C) Microsoft Corporation. All rights reserved.\r\n\r\nWarning : SQM1014: Unable to extract stored procedure 'dbo.sp_upgraddiagrams' from SqlServer. Invalid object name 'dbo.dtproperties'.\r\n"
                         .Equals(readToEnd))
                {
                    Console.WriteLine(readToEnd);
                }
            }
            process.WaitForExit();
        }

        private void qwe5()
        {
            var process =
                new Process
                {
                    StartInfo =
                    {
                        UseShellExecute = false,
                        RedirectStandardOutput = true,
                        FileName = @"C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\SqlMetal.exe",
                        Arguments =
                            string.Format(@"/language:csharp /namespace:PropertyExpression.ReactiveRelation.Test.Demo /context:DataClassesDataContext /pluralize /views /functions /sprocs /code:{0}\..\..\..\ReactiveRelation.Test.Demo\Class1.cs {0}\..\..\..\ReactiveRelation.Test.Demo\Class1.dbml",
                                          Path.GetDirectoryName(GetType().Assembly.Location))
                    }
                };
            process.Start();
            var readToEnd = process.StandardOutput.ReadToEnd();
            if (!string.IsNullOrEmpty(readToEnd))
            {
                if (!"Microsoft (R) Database Mapping Generator 2008 version 1.00.30729\nfor Microsoft (R) .NET Framework version 3.5\r\nCopyright (C) Microsoft Corporation. All rights reserved.\r\n\r\n"
                         .Equals(readToEnd))
                {
                    Console.WriteLine(readToEnd);
                }
            }
            process.WaitForExit();
        }

    }
}


